package com.irdstudio.tdp.console.api.rest;

import java.util.List;

import com.irdstudio.sdk.beans.core.util.UUIDUtil;
import com.irdstudio.tdp.console.constant.Constants;
import com.irdstudio.tdp.console.service.facade.PaasSysTreeService;
import com.irdstudio.tdp.console.service.vo.PaasSysTreeVO;
import com.irdstudio.tdp.console.service.vo.SSubsInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.sdk.beans.core.vo.ResponseData;
import com.irdstudio.sdk.ssm.web.AbstractController;
import com.irdstudio.tdp.console.service.facade.SSubsInfoService;

@RestController
@RequestMapping("/api")
public class SSubsInfoController extends AbstractController  {
	
	@Autowired
	@Qualifier("sSubsInfoService")
	private SSubsInfoService sSubsInfoService;
	@Autowired
	@Qualifier("paasSysTreeServiceImpl")
	private PaasSysTreeService paasSysTreeService;

	
	/**
	 * 列表数据查询
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/s/subs/infos", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SSubsInfoVO>> querySSubsInfoAll(SSubsInfoVO vo) {
		List<SSubsInfoVO> outputVo = sSubsInfoService.queryAllOwner(vo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/subs/info/{subsCode}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SSubsInfoVO> queryByPk(@PathVariable("subsCode") String subsCode) {		
		SSubsInfoVO inVo = new SSubsInfoVO();
				inVo.setSubsCode(subsCode);
		SSubsInfoVO outputVo = sSubsInfoService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param sSubsInfo
	 * @return
	 */
	@RequestMapping(value="/s/subs/info", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SSubsInfoVO inSSubsInfoVo) {		
		int outputVo = sSubsInfoService.deleteByPk(inSSubsInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inSSubsInfoVo
	 * @return
	 */
	@RequestMapping(value="/s/subs/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SSubsInfoVO inSSubsInfoVo) {		
		int outputVo = sSubsInfoService.updateByPk(inSSubsInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inSSubsInfoVo
	 * @return
	 */
	@RequestMapping(value="/s/subs/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSSubsInfo(@RequestBody SSubsInfoVO inSSubsInfoVo) {
		int outputVo = sSubsInfoService.insertSSubsInfo(inSSubsInfoVo);
		// 创建业务系统时 初始化 paas_sys_tree 目录
		this.generatePaasSysTrees(inSSubsInfoVo);
		return getResponseData(outputVo);
		
	}

	/**
	 * 根据业务系统信息 初始 paas_sys_tree
	 * @param sSubsInfo
	 */
	private void generatePaasSysTrees(SSubsInfoVO sSubsInfo) {
		if (sSubsInfo == null) {
			return;
		}
		PaasSysTreeVO root = new PaasSysTreeVO();
		root.setPackageId(sSubsInfo.getSubsCode());
		root.setPackageCode(sSubsInfo.getSubsCode());
		root.setPackageName(sSubsInfo.getSubsName());
		root.setOrderValue(100);
		root.setPackageType(0);
		paasSysTreeService.insertPaasSysTree(root);

		PaasSysTreeVO develop = new PaasSysTreeVO();
		develop.setPackageId(UUIDUtil.getUUID());
		develop.setPackageCode(Constants.PackageType.Develop.name().toLowerCase());
		develop.setPackageName(Constants.PackageType.Develop.getName());
		develop.setPackageAbvId(root.getPackageId());
		develop.setOrderValue(200);
		develop.setPackageType(Constants.PackageType.Develop.getCode());
		paasSysTreeService.insertPaasSysTree(develop);

		PaasSysTreeVO scp = new PaasSysTreeVO();
		scp.setPackageId(UUIDUtil.getUUID());
		scp.setPackageCode(Constants.PackageType.Scp.name().toLowerCase());
		scp.setPackageName(Constants.PackageType.Scp.getName());
		scp.setPackageAbvId(root.getPackageId());
		scp.setOrderValue(300);
		scp.setPackageType(Constants.PackageType.Scp.getCode());
		paasSysTreeService.insertPaasSysTree(scp);

		PaasSysTreeVO app = new PaasSysTreeVO();
		app.setPackageId(UUIDUtil.getUUID());
		app.setPackageCode(Constants.PackageType.App.name().toLowerCase());
		app.setPackageName(Constants.PackageType.App.getName());
		app.setPackageAbvId(root.getPackageId());
		app.setOrderValue(400);
		app.setPackageType(Constants.PackageType.App.getCode());
		paasSysTreeService.insertPaasSysTree(app);

		PaasSysTreeVO appDeploy = new PaasSysTreeVO();
		appDeploy.setPackageId(UUIDUtil.getUUID());
		appDeploy.setPackageCode(Constants.PackageType.AppDeploy.name().toLowerCase());
		appDeploy.setPackageName(Constants.PackageType.AppDeploy.getName());
		appDeploy.setPackageAbvId(app.getPackageId());
		appDeploy.setOrderValue(100);
		appDeploy.setPackageType(Constants.PackageType.AppDeploy.getCode());
		paasSysTreeService.insertPaasSysTree(appDeploy);

		PaasSysTreeVO appLog = new PaasSysTreeVO();
		appLog.setPackageId(UUIDUtil.getUUID());
		appLog.setPackageCode(Constants.PackageType.AppLog.name().toLowerCase());
		appLog.setPackageName(Constants.PackageType.AppLog.getName());
		appLog.setPackageAbvId(app.getPackageId());
		appLog.setOrderValue(200);
		appLog.setPackageType(Constants.PackageType.AppLog.getCode());
		paasSysTreeService.insertPaasSysTree(appLog);



	}

	@RequestMapping(value="/s/subs/info/all", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SSubsInfoVO>> queryAllSSubsInfo(SSubsInfoVO vo) {
		List<SSubsInfoVO> outputVo = sSubsInfoService.queryAllByCondition(vo);
		return getResponseData(outputVo);

	}
	
	/**
	 * 根据用户名获取子系统列表
	 * @param inSSubsInfoVo
	 * @return
	 */
	@RequestMapping(value="/s/user/subs/info/{actorno}", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SSubsInfoVO>> queryUserSSubsInfo(@PathVariable("actorno") String actorno) {
		List<SSubsInfoVO> outputVo = sSubsInfoService.queryUserByCondition(actorno);
		return getResponseData(outputVo);

	}
}
